package com.cms.tenant;

import com.auxiliary.Util;
import com.doman.Tenant;
import com.resultmsg.BaseEnums;
import com.resultmsg.Result;
import com.resultmsg.Results;
import com.server.TenantMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@RequestMapping(value = "/educloud")
@RestController
public class TenantController {



    @Autowired
    private TenantMapper mapper;


    /**
     * 获取租户列表
     * @return
     */
    @RequestMapping(value = "/tenants",method = RequestMethod.GET)
    public Result getTenants(){
        List<Tenant>  list =null;
        try {
           list = mapper.selectAll();
           return Results.successWithData(list,BaseEnums.SUCCESS.code(),BaseEnums.SUCCESS.desc());

        }catch (Exception e){
            return Results.failureWithData(e,BaseEnums.OPERATION_FAILURE.code(),BaseEnums.OPERATION_FAILURE.desc());
        }
    }

    /**
     * 插入租户
     * @param tenant
     * @return
     */
    @RequestMapping(value = "/tenants",method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public Result addTenant(@RequestBody Tenant tenant){
        System.out.println("插入租户");

        int res = -1;
        tenant.setCreateTime(Util.getCurrentTime());
        tenant.setUpdateTime(Util.getCurrentTime());
        if(tenant.getTenantId().isEmpty()){
            tenant.setTenantId(Util.getKcode("",6));
        }
//        int res = mapper.insertSelective(tenant);

        try{
//            res = _addTenant(tenant);
            res = mapper.insertSelective(tenant);

        }catch (Exception e){
            addTenant(tenant);
        }


        if(res>0){
            return Results.successWithData(tenant,BaseEnums.SUCCESS.code(),BaseEnums.SUCCESS.desc());
        }else{
            return Results.failure(BaseEnums.OPERATION_FAILURE.code(),BaseEnums.OPERATION_FAILURE.desc());
        }
    }

    /**
     * 更新租户
     * @param tenant
     * @return
     */
    @RequestMapping(value = "/tenants",method = RequestMethod.PUT, produces = {"application/json;charset=UTF-8"})
    public Result updateTenant(@RequestBody Tenant tenant){
        System.out.println("更新租户");

        tenant.setUpdateTime(Util.getCurrentTime());
        int res = mapper.updateByPrimaryKeySelective(tenant);

        if(res>0){
            return Results.success(BaseEnums.SUCCESS.code(),BaseEnums.SUCCESS.desc());
        }else{
            return Results.failure(BaseEnums.OPERATION_FAILURE.code(),BaseEnums.OPERATION_FAILURE.desc());
        }
    }
    /**
     * 删除租户
     * @param kcode 租户编码
     * @return
     */
    @RequestMapping(value = "/tenants/{kcode}",method = RequestMethod.DELETE)
    public  Result delTenant(@PathVariable("kcode") String kcode){

        int res  = mapper.deleteByKcode(kcode);

        if(res>0){
            return Results.success(BaseEnums.SUCCESS.code(),BaseEnums.SUCCESS.desc());
        }else{
            return Results.failure(BaseEnums.OPERATION_FAILURE.code(),BaseEnums.OPERATION_FAILURE.desc());
        }

    }


    //模糊+搜索
    @RequestMapping("/tenants/userMoHu")
    public  Result userMoHu(Tenant tenant){
        List<Tenant> res  = mapper.selectByPrimaryUserMoHu(tenant);
        if(res.size()> 0){
            return Results.successWithData(res ,BaseEnums.SUCCESS.code(),BaseEnums.SUCCESS.desc());
        }else{
            return Results.failure(BaseEnums.OPERATION_FAILURE.code(),BaseEnums.OPERATION_FAILURE.desc());
        }
    }

    //查询租户信息
    @RequestMapping("/tenants/selecttenant")
    public  Result selecttenant(Tenant tenant){
        Tenant tenant1 = mapper.selectByPrimaryByOrganize(tenant);
        if(tenant1 != null){
            return Results.successWithData(tenant1 ,BaseEnums.SUCCESS.code(),BaseEnums.SUCCESS.desc());
        }else{
            return Results.failure(BaseEnums.OPERATION_FAILURE.code(),BaseEnums.OPERATION_FAILURE.desc());
        }
    }




    }




